package model;

import inf.minife.fe.Constraint;
import inf.minife.fe.DOF;
import inf.minife.fe.Force;
import inf.minife.fe.Model;
import inf.minife.fe.Realtable;
import inf.minife.fe.Truss3D;

/*
 * In this program, we use space truss model
 * with 25 truss elements and 10 nodes
 * follow the model of Rajeev and Krishnamoorthy
 * "Discrete optimization of structure using genetic algorithm"
 * that is defined in Gere and Weaver's 
 * "Analysis of Framed Structures"
 */


/*
 * The set of areas available for this truss
 * according to Rajeev and Krishnamoorthy
 * S = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8
 * 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
 * 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6,
 * 2.8, 3.0, 3.2, 3.4}
 */
public class SpaceTruss {
	
	private Model model;
	
	public static void main(String[] args) {
		Model model = new SpaceTruss().getModel();
		model.printStructure();
		model.solve();
		
		model.printResults();
		
		double mass = model.getTotalMass();
		
		System.out.println("Total Mass = " + mass);
		//Viewer2 viewer = new Viewer2(model);
		//viewer.setVisible(true);
	}
	
	public SpaceTruss(){
		// model
		model = new Model();
		/* Data of Space Truss 
		 * E = 1x10^4 ksi (6.89x10^4 MPa)
		 * rho = 0.10 lb/in^3 (2,770 kg.m^3)
		 * sigma max = +-40,000 psi
		 * u max = +- 0.35 in
		 */
		double E = 1E4; // 10^4 ksi (modulus of elasticity)
		double rho = 0.10; // lb/in^3 (density of steel)
		model.createMaterial(1, E, rho);
		
		double[] arrayA = {	0.1, 0.7, 3.2, 0.1, 1.4, 1.1, 0.5, 3.4};
		
		for (int i = 1; i <= 8; i++) {
			double a = arrayA[i-1];
			//Realtable r = model.createRealtable(i, Truss2D.TYPE);
			//r.setValue(Truss2D.RT_A, A);
			
			Realtable r = model.createRealtable(i, Truss3D.TYPE);
			r.setValue(Truss3D.RT_A, a);
			System.out.println("Element [" + i + "] has area = " + r.getValue(Truss3D.RT_A));
		}
		
		/*
		 * Node - x,y,z
		 * 1: -37.5, 0, 200
		 * 2: 37.5, 0, 200
		 * 3: -37.5, 37.50, 100
		 * 4: 37.5, 37.50, 100
		 * 5: 37.5, -37.5, 100
		 * 6: -37.5, -37.5, 100
		 * 7: -100, 100, 0
		 * 8: 100, 100, 0
		 * 9: 100, -100, 0
		 * 10: -100, -100, 0 
		 */
		
		double val1 = 37.5; 
		double val2 = 100.0;
		double val3 = 200.0;
		model.createNode(1, -val1, 0, val3);
		model.createNode(2, val1, 0, val3);
		model.createNode(3, -val1, val1, val2);
		model.createNode(4, val1, val1, val2);
		model.createNode(5, val1, -val1, val2);
		model.createNode(6, -val1, -val1, val2);
		model.createNode(7, -val2, val2, 0);
		model.createNode(8, val2, val2, 0);
		model.createNode(9, val2, -val2, 0);
		model.createNode(10, -val2, -val2, 0);
		
		/*
		 * Node - Fx, Fy, Fz (lbs)
		 * 1: 1000, -10000, -10000
		 * 2: 0, -10000, -10000
		 * 3: 500, 0, 0
		 * 6: 600, 0, 0
		 */
		// forces
		Force f1 = new Force();
		Force f2 = new Force();
		Force f3 = new Force();
		Force f6 = new Force();
		
		f1.setValue(DOF.T_X, 1000.0); 
		f1.setValue(DOF.T_Y, -10000);
		f1.setValue(DOF.T_Z, -10000);
		
		f2.setValue(DOF.T_Y, -10000);
		f2.setValue(DOF.T_Z, -10000);
		
		f3.setValue(DOF.T_X, 500.0); 
		f6.setValue(DOF.T_X, 600.0);	
		
		
		model.getNode(1).setForce(f1);
		model.getNode(2).setForce(f2);
		model.getNode(3).setForce(f3);
		model.getNode(6).setForce(f6);
		
		/* Constrain x,y,z of nodes 7,8,9,10 */
		// constraints
		Constraint c = new Constraint();
		c.setFree(DOF.T_X, false);
		c.setFree(DOF.T_Y, false);
		c.setFree(DOF.T_Z, false);
		
		model.getNode(7).setConstraint(c);
		model.getNode(8).setConstraint(c);
		model.getNode(9).setConstraint(c);
		model.getNode(10).setConstraint(c);
		
		/*
		 * Space Truss structure:
		 * Group member - Truss
		 * G1: 1-2
		 * G2: 1-4, 2-3, 1-5, 2-6
		 * G3: 2-5, 2-4, 1-3, 1-6
		 * G4: 3-6, 4-5
		 * G5: 3-4, 5-6
		 * G6: 3-10, 6-7, 4-9, 5-8
		 * G7: 3-8, 4-7, 6-9, 5-10
		 * G8: 3-7, 4-8, 5-9, 6-10
		 */
		// 25 elements
		model.createElement(1, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(1), model.getNode(1), model.getNode(2));
		model.createElement(2, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(1), model.getNode(4));
		model.createElement(3, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(2), model.getNode(3));
		model.createElement(4, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(1), model.getNode(5));
		model.createElement(5, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(2), model.getNode(6));
		model.createElement(6, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(2), model.getNode(5));
		model.createElement(7, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(2), model.getNode(4));
		model.createElement(8, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(1), model.getNode(3));
		model.createElement(9, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(1), model.getNode(6));
		model.createElement(10, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(4), model.getNode(3), model.getNode(6));
		model.createElement(11, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(4), model.getNode(4), model.getNode(5));
		model.createElement(12, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(5), model.getNode(3), model.getNode(4));
		model.createElement(13, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(5), model.getNode(5), model.getNode(6));
		model.createElement(14, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(3), model.getNode(10));
		model.createElement(15, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(6), model.getNode(7));
		model.createElement(16, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(4), model.getNode(9));
		model.createElement(17, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(5), model.getNode(8));
		model.createElement(18, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(3), model.getNode(8));
		model.createElement(19, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(4), model.getNode(7));
		model.createElement(20, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(6), model.getNode(9));
		model.createElement(21, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(5), model.getNode(10));
		model.createElement(22, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(3), model.getNode(7));
		model.createElement(23, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(4), model.getNode(8));
		model.createElement(24, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(5), model.getNode(9));
		model.createElement(25, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(6), model.getNode(10));
		
		
	}
	
	public Model createModel(double[] x){
		// model
		model = new Model();
		/* Data of Space Truss 
		 * E = 1x10^4 ksi (6.89x10^4 MPa)
		 * rho = 0.10 lb/in^3 (2,770 kg.m^3)
		 * sigma max = +-40,000 psi
		 * u max = +- 0.35 in
		 */
		double E = 1E4; // 10^4 ksi (modulus of elasticity)
		double rho = 0.10; // lb/in^3 (density of steel)
		model.createMaterial(1, E, rho);
				
		for (int i = 1; i <= x.length; i++) {
			double a = x[i-1];
			
			Realtable r = model.createRealtable(i, Truss3D.TYPE);
			r.setValue(Truss3D.RT_A, a);
			System.out.println("Element [" + i + "] has area = " + r.getValue(Truss3D.RT_A));
		}
		
		/*
		 * Node - x,y,z
		 * 1: -37.5, 0, 200
		 * 2: 37.5, 0, 200
		 * 3: -37.5, 37.50, 100
		 * 4: 37.5, 37.50, 100
		 * 5: 37.5, -37.5, 100
		 * 6: -37.5, -37.5, 100
		 * 7: -100, 100, 0
		 * 8: 100, 100, 0
		 * 9: 100, -100, 0
		 * 10: -100, -100, 0 
		 */
		
		double val1 = 37.5; 
		double val2 = 100.0;
		double val3 = 200.0;
		model.createNode(1, -val1, 0, val3);
		model.createNode(2, val1, 0, val3);
		model.createNode(3, -val1, val1, val2);
		model.createNode(4, val1, val1, val2);
		model.createNode(5, val1, -val1, val2);
		model.createNode(6, -val1, -val1, val2);
		model.createNode(7, -val2, val2, 0);
		model.createNode(8, val2, val2, 0);
		model.createNode(9, val2, -val2, 0);
		model.createNode(10, -val2, -val2, 0);
		
		/*
		 * Node - Fx, Fy, Fz (lbs)
		 * 1: 1000, -10000, -10000
		 * 2: 0, -10000, -10000
		 * 3: 500, 0, 0
		 * 6: 600, 0, 0
		 */
		// forces
		Force f1 = new Force();
		Force f2 = new Force();
		Force f3 = new Force();
		Force f6 = new Force();
		
		f1.setValue(DOF.T_X, 1000.0); 
		f1.setValue(DOF.T_Y, -10000);
		f1.setValue(DOF.T_Z, -10000);
		
		f2.setValue(DOF.T_Y, -10000);
		f2.setValue(DOF.T_Z, -10000);
		
		f3.setValue(DOF.T_X, 500.0); 
		f6.setValue(DOF.T_X, 600.0);	
		
		
		model.getNode(1).setForce(f1);
		model.getNode(2).setForce(f2);
		model.getNode(3).setForce(f3);
		model.getNode(6).setForce(f6);
		
		/* Constrain x,y,z of nodes 7,8,9,10 */
		// constraints
		Constraint c = new Constraint();
		c.setFree(DOF.T_X, false);
		c.setFree(DOF.T_Y, false);
		c.setFree(DOF.T_Z, false);
		
		model.getNode(7).setConstraint(c);
		model.getNode(8).setConstraint(c);
		model.getNode(9).setConstraint(c);
		model.getNode(10).setConstraint(c);
		
		/*
		 * Space Truss structure:
		 * Group member - Truss
		 * G1: 1-2
		 * G2: 1-4, 2-3, 1-5, 2-6
		 * G3: 2-5, 2-4, 1-3, 1-6
		 * G4: 3-6, 4-5
		 * G5: 3-4, 5-6
		 * G6: 3-10, 6-7, 4-9, 5-8
		 * G7: 3-8, 4-7, 6-9, 5-10
		 * G8: 3-7, 4-8, 5-9, 6-10
		 */
		// 25 elements
		model.createElement(1, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(1), model.getNode(1), model.getNode(2));
		model.createElement(2, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(1), model.getNode(4));
		model.createElement(3, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(2), model.getNode(3));
		model.createElement(4, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(1), model.getNode(5));
		model.createElement(5, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(2), model.getNode(2), model.getNode(6));
		model.createElement(6, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(2), model.getNode(5));
		model.createElement(7, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(2), model.getNode(4));
		model.createElement(8, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(1), model.getNode(3));
		model.createElement(9, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(3), model.getNode(1), model.getNode(6));
		model.createElement(10, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(4), model.getNode(3), model.getNode(6));
		model.createElement(11, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(4), model.getNode(4), model.getNode(5));
		model.createElement(12, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(5), model.getNode(3), model.getNode(4));
		model.createElement(13, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(5), model.getNode(5), model.getNode(6));
		model.createElement(14, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(3), model.getNode(10));
		model.createElement(15, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(6), model.getNode(7));
		model.createElement(16, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(4), model.getNode(9));
		model.createElement(17, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(6), model.getNode(5), model.getNode(8));
		model.createElement(18, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(3), model.getNode(8));
		model.createElement(19, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(4), model.getNode(7));
		model.createElement(20, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(6), model.getNode(9));
		model.createElement(21, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(7), model.getNode(5), model.getNode(10));
		model.createElement(22, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(3), model.getNode(7));
		model.createElement(23, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(4), model.getNode(8));
		model.createElement(24, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(5), model.getNode(9));
		model.createElement(25, Truss3D.TYPE, model.getMaterial(1), 
				model.getRealtable(8), model.getNode(6), model.getNode(10));
		
		return model;
	}
	public Model getModel(){
		return model;
	}
}
